﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.MerchantsSettings
{
    public class WxMsgController : Controller
    {
        //
        // GET: /WxMsg/
        //服务
        ServiceReference1.Service1Client wsdcser = new ServiceReference1.Service1Client();

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取系统的增删查改的权限
        /// </summary>
        /// <returns></returns>
        public ActionResult getPower()
        {
            try
            {
                string[] PowerS = null;
                //string RetPower = "";
                Models.ControlPower RetPower = new Models.ControlPower();
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];
                if (user != null)
                {
                    PowerS = user.RolePower.Split(',');//权限码
                    //拼接增删查改的权限
                    foreach (string pow in PowerS)
                    {
                        if (pow == "009011002")
                        {
                            RetPower.Add = 1;
                        }
                        else if (pow == "009011003")
                        {
                            RetPower.Dell = 1;
                        }
                        else if (pow == "009011004")
                        { RetPower.Edit = 1; }
                    }
                }
                else
                {                  
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面                   
                }

                return Json(RetPower);
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 获取当前操作员拥有商户的权限
        /// </summary>
        /// <returns></returns>
        public ActionResult getMer(JqGridParam jqgridparam)
        {
            try
            {
                Page<Model.Order_Mer_T> page = new Page<Model.Order_Mer_T>();
                List<Model.Order_Mer_T> list = new List<Model.Order_Mer_T>();//定义商品分类设置
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }
                string result = wsdcser.GetMer(user.OperID, "", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        foreach (JToken item in jieguo)
                        {
                            Model.Order_Mer_T m = new Model.Order_Mer_T();
                            m.MerID = item["MerID"].ToString();
                            m.MerName = item["MerName"].ToString();
                            m.MerAdr = item["MerAdr"].ToString();
                            m.MerCall = item["MerCall"].ToString();
                            m.MerTxt = item["MerTxt"].ToString();
                            list.Add(m);
                        }
                    }
                    jqgridparam.records = list.Count;//查询的数据总数
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = page.FindListPage(list, ref jqgridparam)//返回当前页的数据
                    };
                    return Json(data);
                }
                return Json(searcho["Msg"].ToString());
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 获取商户的所有微信通知号
        /// </summary>
        /// <param name="MerID"></param>
        /// <param name="jqgridparam"></param>
        /// <returns></returns>
        public ActionResult getgrid(string MerID, JqGridParam jqgridparam)
        {
            try
            {
                Page<Model.Order_MerSetWxMess_T> page =  new Page<Model.Order_MerSetWxMess_T>();//分页类定义
                List<Model.Order_MerSetWxMess_T> list = new List<Model.Order_MerSetWxMess_T>();
                string result = wsdcser.GetMerWxID(MerID, GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        list = ParseClass<Model.Order_MerSetWxMess_T>.JsonList(jieguo);//解析返回的结果\
                    }
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = list
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = "0",
                        rows = list
                    };
                    return Json(data);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        #region 增删改微信通知号
        /// <summary>
        /// 增、删、改商品信息
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateWxMsg(int Tag, Model.Order_MerSetWxMess_T Order_MerSetWxMess)
        {
            try
            {
                string result = "";
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                #region 转换传入的商品信息
                Model.Order_MerSetWxMess_T New_WxMsg = new Model.Order_MerSetWxMess_T();
                New_WxMsg.ID = Order_MerSetWxMess.ID;
                New_WxMsg.OpenID = Order_MerSetWxMess.OpenID;
                New_WxMsg.MerID = Order_MerSetWxMess.MerID;
                New_WxMsg.OpenName = Order_MerSetWxMess.OpenName;
                New_WxMsg.Stat = Order_MerSetWxMess.Stat;
                New_WxMsg.WxMesCall = Order_MerSetWxMess.WxMesCall;
                New_WxMsg.YZM = Order_MerSetWxMess.YZM;
                New_WxMsg.YZMTime = Order_MerSetWxMess.YZMTime;
                #endregion
                string a = New_WxMsg.ToJson();
                result = wsdcser.Set_MerWxID(a, Tag, GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();
                if (bs == "1")
                {
                    return Content(bs);
                }
                else
                {
                    return Content(searcho["Msg"].ToString());
                }
            }
            catch (Exception ex)
            {
                return Content("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

    }
}
